class Utils {
    verification(msg, arr) {
        for (let item of arr) {
            let key = item[0];
            let type = item[1];
            let value = msg[key]
            if (typeof value !== type) { //类型验证 string ,number, Boolean, undefined, null, object
                return key;
            }
            if (type === 'string') {
                if (value.length === 0) {
                    return key;
                } else if (item[2]) {
                    let re = item[2];
                    if (!re.test(value)) { //正则验证
                        return key;
                    }
                }
            } else if (type === 'number') {
                if (value < 0) { //不能小于0
                    return key;
                }
            }
        }
        return
    }

    //获得随机一组不重复的数字
    getRandomArray(num, array) {
        let arrayCopy = array.slice(0)
        let length = arrayCopy.length
        var k = []
        for (var i = 0; i < num; i++) {
            var l = Math.floor(Math.random() * length);
            length = length - 1
            k.push(arrayCopy[l])
            arrayCopy[l] = arrayCopy[length]
        }
        return k
    }

    getOneFromArray(array) {
        return array[Math.floor(Math.random() * array.length)]
    }

    getRandomNumber(min, max) {
        max = Math.floor(max)
        min = Math.floor(min)
        let n = max + min
        let x1 = Math.floor(Math.random() * (max - min + 1) + min)
        let x2 = Math.floor(Math.random() * (max - min + 1) + min)
        let x3 = x1 + x2
        let m = x3 > n ? x3 : 2 * n - x3
        return m - max
    }

    getRandom(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }
}

let utils = new Utils();

module.exports = utils;